//
//  BGUpLoadFileTool.m
//  bigMall
//
//  Created by LTOVE on 16/10/9.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGUpLoadFileTool.h"
#import "AFNetworking.h"
#import "BGFileUpLoadModel.h"
#import "MJExtension.h"
#import "BGBasicUrl.h"
@implementation BGUpLoadFileTool

+ (void)upLoadPicWithImageDataArray:(NSArray *)dataArray success:(void (^)(NSArray *, NSArray *))success failure:(void (^)(NSError *))failure
{
//    NSMutableArray *FilePathArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"%@/Api_1/Index/upload",[BGBasicUrl getBasicUrl]];
//     NSString *url = @"http://thinkpro.local/Api_1/Index/upload";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (int i = 0; i < dataArray.count; i ++) {
            
            NSData *data = UIImageJPEGRepresentation(dataArray[i], 1.0);
            [formData appendPartWithFileData:data name:[NSString stringWithFormat:@"ios_%d",i] fileName:[NSString stringWithFormat:@"ios_%d.png",i] mimeType:@"image/png"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        BGUpLoadFileToolModel *resultModel = [BGUpLoadFileToolModel objectWithKeyValues:responseObject];
        if (success) {
            success(resultModel.arr,resultModel.idStrArray);
        }
//        __block FilePathArray = [NSMutableArray arrayWithArray:resultModel.arr];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failure) {
            failure(error);
        }
    }];
    
}

@end
#pragma mark- 华丽分割线--下边是模型部分
@implementation BGUpLoadFileToolModel

+ (NSDictionary *)replacedKeyFromPropertyName
{
    return @{
             @"arr":@"res.arr",
             
             };
}

+ (NSDictionary *)objectClassInArray
{
    return @{
             
             @"arr":@"BGUpLoadFileToolModelArr"
             
             };
    
}

- (NSArray *)arr
{
    NSMutableArray *filePathStrArray = [NSMutableArray array];
    
    for (BGUpLoadFileToolModelArr *model in _arr) {
        [filePathStrArray addObject:model.url];
    }
    
    return filePathStrArray;
}

- (NSArray *)idStrArray
{
    NSMutableArray *idStrArr = [NSMutableArray array];
    for (BGUpLoadFileToolModelArr *model in _arr) {
        [idStrArr addObject:model.idStr];
    }
    return idStrArr;
}
@end

@implementation BGUpLoadFileToolModelArr

+ (NSDictionary *)replacedKeyFromPropertyName
{
    return @{
             @"idStr":@"id",
             
             };
}

@end